<?php

namespace Modules\Ui\utils;

use Illuminate\Contracts\Support\Renderable;

class Helper
{
    /**
     * 把给定的值转化为字符串.
     *
     * @param  string|\Closure|Renderable|Htmlable  $value
     * @param array $params
     * @param  mixed  $newThis
     * @return string
     */
    public static function render($value, array $params = [], $newThis = null): string
    {
        if (is_string($value)) {
            return $value;
        }

        if ($value instanceof \Closure) {
            $newThis && ($value = $value->bindTo($newThis));

            $value = $value(...(array) $params);
        }

        if ($value instanceof Renderable) {
            return (string) $value->render();
        }

        if ($value instanceof Htmlable) {
            return (string) $value->toHtml();
        }

        return (string) $value;
    }


}
